﻿package com.kingdee.eas.custom.bpmworkflow.clgs.clyhjh;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

import javax.ejb.*;
import javax.xml.rpc.ServiceException;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.EAS_SUNAC_235_MaterialPurchasingPlan;
import com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.EAS_SUNAC_236_MaterialPurchasingPlan;
import com.ekingwin.www.esb.header.v1.Header;
import com.ekingwin.www.esb.header.v1.holders.HeaderHolder;
import com.kingdee.bos.*;
import com.kingdee.bos.ui.face.UIRuleUtil;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.base.permission.UserFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.bpmworkflow.BPMConfigCollection;
import com.kingdee.eas.custom.bpmworkflow.BPMConfigFactory;
import com.kingdee.eas.custom.bpmworkflow.BPMConfigInfo;
import com.kingdee.eas.custom.bpmworkflow.util.BPMViewUrlUtil;
import com.kingdee.eas.custom.bpmworkflow.util.LogUtil;
import com.kingdee.eas.custom.bpmworkflow.util.ProcInstHelp;
import com.kingdee.eas.fdc.finance.McPayoutBillFactory;
import com.kingdee.eas.fdc.finance.McPayoutBillInfo;
import com.kingdee.eas.fdc.invite.strategymaterial.IMaterialPurchasingPlan;
import com.kingdee.eas.fdc.invite.strategymaterial.MaterialPurchasingPlanFactory;
import com.kingdee.eas.fdc.invite.strategymaterial.MaterialPurchasingPlanInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class MaterialPurchasingPlanRequestFacadeControllerBean extends AbstractMaterialPurchasingPlanRequestFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.bpmworkflow.clgs.clyhjh.MaterialPurchasingPlanRequestFacadeControllerBean");
    
    
    @Override
    protected String _createBillXML(Context ctx, String billId, String type)
    		throws BOSException {
    	// TODO Auto-generated method stub
    	StringBuffer XML = new StringBuffer();
    	
		String CBZlxySql = getBtsql(billId);
		IRowSet btRowset = DbUtil.executeQuery(ctx, CBZlxySql); //材料公司要货计划表头SQL
    	
		String btEntrysql = getBtentrysql(billId);
		IRowSet btentrySQL = DbUtil.executeQuery(ctx, btEntrysql);//材料公司要货计划分录SQL
		
		String materialGroupSql = getMaterialGroupSql(billId);
		IRowSet materialGroupSqlRowset = DbUtil.executeQuery(ctx, materialGroupSql);//材料公司要货计划动态分录SQL
    	
		ArrayList<String> listNumber = new ArrayList();
    	
    	if(btRowset.size()>0){
    		XML.append("<DATA>");
    		try {
				while(btRowset.next()){
					
					String summitUserNum = ProcInstHelp.getSummitUserNum(ctx,"CLTOBPM", BOSUuid.read("0i7pmzP6QheN8cRRXJ1Dm8znrtQ="));//流程发起人
					UserInfo userinfo = UserFactory.getLocalInstance(ctx).getUserInfo("where number ='"+summitUserNum+"'");
					
					Map orgmap = BPMViewUrlUtil.getCurrentUserDep(ctx,userinfo.getId().toString());//部门
					Map postmap = BPMViewUrlUtil.getCurrentUserPost(ctx,userinfo.getId().toString());//岗位
					String SubmitUserOrgNum=(String) orgmap.get("FcreatorDepNum");//登录人所在部门编码
					String SubmitUserOrgName=(String) orgmap.get("FcreatorDepName");//登录人所在部门名称
					String SubmitUserPostNum=(String) postmap.get("PositionNum");//提交人所在岗位名称
					String SubmitUserPostName=(String) postmap.get("PositionName");//提交人所在岗位编码

					XML.append("<SummitUserName>").append(userinfo.getName()).append("</SummitUserName>");//提交人名称
					XML.append("<SummitUserNum>").append(userinfo.getNumber()).append("</SummitUserNum>");//提交人编码
					XML.append("<SubmitUserOrgName>").append(SubmitUserOrgName).append("</SubmitUserOrgName>");//提交人所在部门名称
					XML.append("<SubmitUserOrgNum>").append(SubmitUserOrgNum).append("</SubmitUserOrgNum>");//提交人所在部门编码
					XML.append("<CurOrgNum>").append("000101.18").append("</CurOrgNum>");//当前登录组织编码
					XML.append("<SubmitUserPostName>").append(SubmitUserPostName).append("</SubmitUserPostName>");//提交人所在岗位名称
					XML.append("<SubmitUserPostNum>").append(SubmitUserPostNum).append("</SubmitUserPostNum>");//提交人所在岗位编码
					XML.append("<FSubject>").append("请处理材料公司要货计划流程：").append(btRowset.getString("fproname")).append("/").append(btRowset.getString("fnumber")).append("</FSubject>");//单据主题
					XML.append("<BILLNUMBER>").append(btRowset.getString("fnumber")).append("</BILLNUMBER>");//单据编码

					XML.append("<isSubmit>" + type + "</isSubmit>");//标识数据版本
					
					String fconnum = UIRuleUtil.getString(btRowset.getString("fconnum"));
					String fconname = UIRuleUtil.getString(btRowset.getString("fconname"));
					String fproname = UIRuleUtil.getString(btRowset.getString("fproname"));
					String fnumber = UIRuleUtil.getString(btRowset.getString("fnumber"));
					String perName = UIRuleUtil.getString(btRowset.getString("perName"));
					String fsuppname = UIRuleUtil.getString(btRowset.getString("fsuppname"));
					String FReceiver = UIRuleUtil.getString(btRowset.getString("FReceiver"));
					String FAddressWay = UIRuleUtil.getString(btRowset.getString("FAddressWay"));
					String fbizdate = UIRuleUtil.getString(btRowset.getString("fbizdate"));
					String FAddress = UIRuleUtil.getString(btRowset.getString("FAddress"));
					String FEndDate = UIRuleUtil.getString(btRowset.getString("FEndDate"));
					String FFhState = UIRuleUtil.getString(btRowset.getString("FFhState"));
					
					XML.append("<fconnum>" + fconnum + "</fconnum>");
					XML.append("<fconname>" + fconname + "</fconname>");
					XML.append("<fproname>" + fproname + "</fproname>");
					XML.append("<fnumber>" + fnumber + "</fnumber>");
					XML.append("<perName>" + perName + "</perName>");
					XML.append("<fsuppname>" + fsuppname + "</fsuppname>");
					XML.append("<FReceiver>" + FReceiver + "</FReceiver>");
					XML.append("<FAddressWay>" + FAddressWay + "</FAddressWay>");
					XML.append("<fbizdate>" + fbizdate + "</fbizdate>");
					XML.append("<FAddress>" + FAddress + "</FAddress>");
					XML.append("<FEndDate>" + FEndDate + "</FEndDate>");
					XML.append("<FFhState>" + FFhState + "</FFhState>");
					
					XML.append("<YRRE_S_VENDOR_DIV>");
					XML.append("<ITEM>");
					int headrow = 1;
					String ffieldname = null;
					String value =null;
					if (materialGroupSqlRowset.size() > 0) {
						while (materialGroupSqlRowset.next()) {
							ffieldname = materialGroupSqlRowset.getString("ffieldname");
							String fmappingfiled = materialGroupSqlRowset.getString("fmappingfiled");
							listNumber.add(fmappingfiled);
							if(ffieldname!=null){
								if(ffieldname.equals("价格")){
									continue;
								}
								XML.append("<Val"+headrow+">").append(ffieldname).append("</Val"+headrow+">");
								headrow++;
							}
						}
						if(ffieldname!=null){
							XML.append("<value30>").append("价格").append("</value30>");
							XML.append("<FContractQty>").append("合同数量").append("</FContractQty>");
							XML.append("<FPlanQty>").append("计划要货数量").append("</FPlanQty>");
						}
					}
					XML.append("</ITEM>");
					if (btentrySQL.size() > 0) {
						while (btentrySQL.next()) {
							XML.append("<ITEM>");
							for (int i = 0; i < listNumber.size(); i++) {
								value = listNumber.get(i);
								if(value != null){
									String values = UIRuleUtil.getString(btentrySQL.getString(listNumber.get(i)));
									if(listNumber.get(i).equals("value30")) {
										continue;
									}
									XML.append("<Val"+i+">").append(values.replaceAll("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")).append("</Val"+i+">");
								}
							}
							String value30 = UIRuleUtil.getString(btentrySQL.getString("value30"));
							String FContractQty = UIRuleUtil.getString(btentrySQL.getString("FContractQty"));
							String FPlanQty = UIRuleUtil.getString(btentrySQL.getString("FPlanQty"));
							if(value != null){
								XML.append("<value30>" + value30 + "</value30>");// 价格
								XML.append("<FContractQty>" + FContractQty + "</FContractQty>");// 合同数量
								XML.append("<FPlanQty>" + FPlanQty + "</FPlanQty>");// 计划要货数量
							}
							XML.append("</ITEM>");
						}
					}
					XML.append("</YRRE_S_VENDOR_DIV>");
				}
				String attachmentXml = ProcInstHelp.getAttachmentXml(ctx,billId);//获取附件xml
				XML.append(attachmentXml);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			XML.append("</DATA>");
    	}else {
			return "无材料公司要货计划单据信息发送！";
		}
		return XML.toString().replaceAll("null", "").replace("'", "&apos;").replace("\"", "&quot;");
    }
    
    public String getBtsql(String billId){
    	StringBuffer btsql = new StringBuffer();
    	btsql.append(" select conbill.FNEWNUMBER  fconnum /*合同编码*/,conbill.fname fconname/*合同名称*/,pro.fname_l2 fproname/*项目名称*/,bill.fnumber fnumber/*要货计划编码*/,");
    	btsql.append(" person.fname_l2 perName/*经办人*/,supp.fname_l2 fsuppname /*总包单位*/,bill.FReceiver FReceiver/*现场收货人*/,bill.FAddressWay FAddressWay/*收货人联系方式*/,to_char(bill.FBizDate,'yyyy-MM-dd') fbizdate/*录入日期*/,bill.FAddress FAddress/*收货人地址*/,");
    	btsql.append(" to_char(bill.FEndDate,'yyyy-MM-dd') FEndDate/*要求到货时间*/,case bill.FFhState when 10 then '未发货' when 20 then '部门发货' when 30 then '完全发货' end FFhState/*发货状态*/");
    	btsql.append(" from T_FDC_MaterialPurchasingPlan bill");
    	btsql.append(" left join T_CON_ContractBill conbill on conbill.fid = bill.FCONTRACTNUMBERID");
    	btsql.append(" left join T_FDC_CurProject pro on pro.fid = bill.FCurProjectID");
    	btsql.append(" left join T_PM_User person on person.fid = bill.FCreatorID ");
    	btsql.append(" left join T_PM_User shr on shr.fid = bill.FAuditorID");
    	btsql.append(" left join T_FDC_SupplierStock supp on supp.fid= bill.FContractorUnitID ");
    	btsql.append(" where bill.fid = '"+billId+"'");
    	return btsql.toString();
    }
    
    public String getBtentrysql(String billId){
    	StringBuffer gysEntrySql = new StringBuffer();
    	gysEntrySql.append(" select FValue1 value1,FValue2 value2,FValue3 value3,FValue4 value4,FValue5 value5,FValue6 value6,FValue7 value7,FValue8 value8,FValue9 value9,FValue10 value10,FValue11 value11,FValue12 value12,");
    	gysEntrySql.append(" FValue13 value13,FValue14 value14,FValue15 value15,FValue16 value16,FValue17 value17,FValue18 value18,FValue19 value19,FValue20 value20,FValue21 value21,FValue22 value22,FValue23 value23,");
    	gysEntrySql.append(" FValue24 value24,FValue25 value25,FValue26 value26,FValue27 value27,FValue28 value28,FValue29 value29,FValue30 value30,");
    	gysEntrySql.append(" entry.FContractQty FContractQty/*合同数量*/,entry.FPlanQty FPlanQty/*计划要货数量*/");
    	gysEntrySql.append(" from T_FDC_MaterialPPE  entry  ");
    	gysEntrySql.append(" left join T_FDC_MaterialPurchasingPlan ysd on entry.fparentid = ysd.fid");
    	gysEntrySql.append(" where ysd.fid = '"+billId+"'");
    	gysEntrySql.append(" order by entry.fseq");
    	return gysEntrySql.toString();
    }
    
	private String getMaterialGroupSql(String billID) {
		StringBuffer entrySql = new StringBuffer();
		entrySql.append(" select pzbfl.ffieldname ffieldname/*字段名称*/,pzbfl.fmappingfiled fmappingfiled/*对应数据库字段*/ from T_FDC_MaterialPurchasingPlan MaterYh ");
		entrySql.append(" left join T_CON_ContractBill ht on ht.fid = MaterYh.FContractNumberID ");
		entrySql.append(" left join T_INV_StrategyPact zlxy on zlxy.fid = ht.FStrategyPactID ");
		entrySql.append(" left join T_BD_MaterialGroup wlfl on wlfl.fid = zlxy.FMaterialTypeID ");
		entrySql.append(" left join T_FDC_MaterialConfiguration pzb on pzb.FMaterialClassifyI = wlfl.fid ");
		entrySql.append(" left join T_FDC_MaterialCE pzbfl on pzbfl.fparentid = pzb.fid ");
		entrySql.append(" where MaterYh.fid ='" + billID + "'  order by fseq ");
		return entrySql.toString();
	}
    
    @Override
    protected void _saveBPMWorkFlow(Context ctx, String billId,
    		String procInstId, String procURL, String success)
    		throws BOSException {
    	// TODO Auto-generated method stub
    	String updateSql = "";
		if("1".equals(success)) {										
			updateSql = " update T_FDC_MaterialPurchasingPlan set CFiProcInstID='"+procInstId+"' ,CFProcURL='"+procURL+"' where fid='"+billId+"'";
			DbUtil.execute(ctx, updateSql);
		}
    }
    
    @Override
    protected String _startBPMWorkFlow(Context ctx, String billId, String type)
    		throws BOSException {
    	// TODO Auto-generated method stub
    	// 生成bpm发起流程页面的url
		String url = "";
		// 调用bpm发起流程接口，传输单据信息
		// 生成bpm发起流程页面的url
		String URL = "http://192.168.2.110/Workflow/MTStart2.aspx";
		String ESBURL = "";
		String BSID = "EAS";
		String BTID = "CL016";
		String STRXML = "";
		String PROCLNSTID = "";
		String ACCOUNT = "ESB";
		String PASSWORD = "ABC23";
		String esbUrl = "";
		// 获取配置信息
		BPMConfigCollection configCol = BPMConfigFactory.getLocalInstance(ctx)
		.getBPMConfigCollection();
		if (configCol.size() > 0) {
			BPMConfigInfo configInfo = configCol.get(0);
			String startURL = configInfo.getBPMStartURL(); // BPM发起流程URL基址
			String esbAccount = configInfo.getESBAccount(); // 账号
			String esbPassword = configInfo.getESBPassword(); // 密码
			esbUrl = ProcInstHelp.getstartwebservice(ctx, "CL016");// ESB
			// webservice地址
			if (startURL != null && !"".equals(startURL)) {
				URL = startURL;
			}
			if (esbAccount != null && !"".equals(esbAccount)) {
				ACCOUNT = esbAccount;
			}
			if (esbPassword != null && !"".equals(esbPassword)) {
				PASSWORD = esbPassword;
			}
			if (esbUrl != null && !"".equals(esbUrl)) {
				ESBURL = esbUrl;
			}
		}

		String glHtjsXML = _createBillXML(ctx, billId, type);
		PROCLNSTID = ProcInstHelp.getBillPROCLNSTID(ctx, billId,"T_FDC_MaterialPurchasingPlan");
		BTID = "CL016";
		STRXML = glHtjsXML;
		logger.info(STRXML);
		
		String time = new SimpleDateFormat("yyyyMMddHHmmssSSS")
		.format(new Date());

		MaterialPurchasingPlanInfo materialPurchasingPlanInfo = null;
		try {
			ObjectUuidPK uuidPK = new ObjectUuidPK(BOSUuid.read(billId));
			materialPurchasingPlanInfo = MaterialPurchasingPlanFactory.getLocalInstance(ctx).getMaterialPurchasingPlanInfo(uuidPK);
		} catch (EASBizException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		EAS_SUNAC_235_MaterialPurchasingPlan parameters = new EAS_SUNAC_235_MaterialPurchasingPlan();
		com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.ITEM[] items = new com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.ITEM[1];
		com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.ITEM item = new com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.ITEM();
		item.setBDID(billId);
		item.setBTID(BTID);
		item.setBSID(BSID);
		item.setSTRXML(STRXML);
		item.setPROCLNSTID(PROCLNSTID);
		item.setUSERID(ctx.getUserName());
		if(materialPurchasingPlanInfo.getName() != null && !materialPurchasingPlanInfo.getName().equals("")){
			item.setRESERVE1(materialPurchasingPlanInfo.getName());
		}else{
			item.setRESERVE1("材料公司要货计划");
		}
		if(materialPurchasingPlanInfo.getNumber() != null && !materialPurchasingPlanInfo.getNumber().equals("")){
			item.setRESERVE2(materialPurchasingPlanInfo.getNumber());
		}else{
			item.setRESERVE2("");
		}
		item.setRESERVE3("");
		item.setRESERVE4(billId);
		item.setRESERVE5("");
		items[0] = item;
		parameters.setLIST(items);

		Header header = new Header();
		header.setACCOUNT(ACCOUNT);
		header.setPASSWORD(PASSWORD);
		header.setBIZTRANSACTIONID("EAS_SUNAC_235_" + billId + time);
		header.setCONSUMER("EAS");
		header.setCOUNT("1");
		header.setSRVLEVEL(Calendar.getInstance());
		HeaderHolder holder = new HeaderHolder(header);

		com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.EAS_SUNAC_235_MaterialPurchasingPlan_pttBindingQSServiceLocator locator = new com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.EAS_SUNAC_235_MaterialPurchasingPlan_pttBindingQSServiceLocator();
		try {
			if (ESBURL != null && !"".equals(ESBURL))
				locator
				.setEAS_SUNAC_235_MaterialPurchasingPlan_pttBindingQSPortEndpointAddress(ESBURL);
			com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.Response rep = locator
			.getEAS_SUNAC_235_MaterialPurchasingPlan_pttBindingQSPort()
			.EAS_SUNAC_235_MaterialPurchasingPlan(parameters, holder);
			String result = rep.getHEADER().getRESULT();
			
			LogUtil.saveCommBPMLog(ctx, "EAS", BTID, billId, "", "", "EAS_SUNAC_235_"+billId+time, "RESULT:"+result,"材料公司要货计划:startBPMWorkFlow");
			
			if ("0".equals(result)) { // 成功
				String encodeBillId = URLEncoder.encode(billId, "UTF-8");
				String token = new BASE64Encoder().encode(ctx.getUserName()
						.getBytes());
				URL = URL + "?BSID=" + BSID + "&BTID=" + BTID + "&BOID="
				+ encodeBillId + "&token=" + token;
			} else { // 失败
				LogUtil.saveCommBPMLog(ctx, "EAS", BTID, billId, "", "", "EAS_SUNAC_235_"+billId+time, "错误信息:"+rep.getHEADER().getCOMMENTS(),"材料公司要货计划:startBPMWorkFlow");
				return "ERROR";
			}
		} catch (com.ekingwin.www.esb.EAS_SUNAC_235_MaterialPurchasingPlan.QueryFault e) {
			e.printStackTrace();
			return "ERROR";
		} catch (RemoteException e) {
			e.printStackTrace();
			return "ERROR";
		} catch (ServiceException e) {
			e.printStackTrace();
			return "ERROR";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "ERROR";
		}catch (EASBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return URL;
    }
    
    @Override
    protected String _stopBPMWorkFlow(Context ctx, String billId)
    		throws BOSException {
    	// TODO Auto-generated method stub
    	String bosType = BOSUuid.read(billId).getType().toString();
		String ESBStopURL = ""; // ESB废弃流程url地址
		String BSID = "EAS";
		String BTID = "CL016";
		String PROCLNSTID = "";
		String ACCOUNT = "ESB";
		String PASSWORD = "ABC23";
		String RESULT = "ERROR";

		// 获取配置信息
		BPMConfigCollection configCol = BPMConfigFactory.getLocalInstance(ctx)
		.getBPMConfigCollection();
		if (configCol.size() > 0) {
			BPMConfigInfo configInfo = configCol.get(0);
			StringBuffer sql = new StringBuffer();
			ESBStopURL = ProcInstHelp.getstopwebservice(ctx, "CL016"); // esb
			// 停止工作流地址
			ACCOUNT = configInfo.getESBAccount(); // 账号
			PASSWORD = configInfo.getESBPassword(); // 密码

			PROCLNSTID = ProcInstHelp.getBillPROCLNSTID(ctx, billId,"T_FDC_MaterialPurchasingPlan");
			BTID = "CL016";

			MaterialPurchasingPlanInfo materialPurchasingPlanInfo = null;
			try {
				ObjectUuidPK uuidPK = new ObjectUuidPK(BOSUuid.read(billId));
				materialPurchasingPlanInfo = MaterialPurchasingPlanFactory.getLocalInstance(ctx).getMaterialPurchasingPlanInfo(uuidPK);
			} catch (EASBizException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			EAS_SUNAC_236_MaterialPurchasingPlan parameters= new EAS_SUNAC_236_MaterialPurchasingPlan();
			com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.ITEM[] items = new com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.ITEM[1];
			com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.ITEM item = new com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.ITEM();
			item.setBDID(billId);
			item.setBTID(BTID);
			item.setBSID(BSID);
			item.setPROCLNSTID(PROCLNSTID);
			item.setUSERID(ctx.getUserName());
			item.setInvalidComment("");
			if(materialPurchasingPlanInfo.getName() != null && !materialPurchasingPlanInfo.getName().equals("")){
				item.setRESERVE1(materialPurchasingPlanInfo.getName());
			}else{
				item.setRESERVE1("材料公司要货计划单据");
			}
			if(materialPurchasingPlanInfo.getNumber() != null && !materialPurchasingPlanInfo.getNumber().equals("")){
				item.setRESERVE2(materialPurchasingPlanInfo.getNumber());
			}else{
				item.setRESERVE2("");
			}
			item.setRESERVE3("");
			item.setRESERVE4(billId);
			item.setRESERVE5("");
			items[0] = item;
			parameters.setLIST(items);

			String time = new SimpleDateFormat("yyyyMMddHHmmssSSS")
			.format(new Date());

			Header header = new Header();
			header.setACCOUNT(ACCOUNT);
			header.setPASSWORD(PASSWORD);
			header.setBIZTRANSACTIONID("EAS_SUNAC_236_" + billId + time);
			header.setCONSUMER("EAS");
			header.setCOUNT("1");
			header.setSRVLEVEL(Calendar.getInstance());
			HeaderHolder holder = new HeaderHolder(header);

			com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.EAS_SUNAC_236_MaterialPurchasingPlan_pttBindingQSServiceLocator locator = new com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.EAS_SUNAC_236_MaterialPurchasingPlan_pttBindingQSServiceLocator();
			if (ESBStopURL != null && !"".equals(ESBStopURL)) {
				locator
				.setEAS_SUNAC_236_MaterialPurchasingPlan_pttBindingQSPortEndpointAddress(ESBStopURL);
			}
			try {
				com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.Response reponse = locator
				.getEAS_SUNAC_236_MaterialPurchasingPlan_pttBindingQSPort()
				.EAS_SUNAC_236_MaterialPurchasingPlan(parameters, holder);
				RESULT = reponse.getHEADER().getRESULT();
				
				LogUtil.saveCommBPMLog(ctx, "EAS", BTID, billId, "", "", "EAS_SUNAC_236_"+billId+time, "RESULT:"+RESULT,"材料公司收货单:stopBPMWorkFlow");
				
				if ("0".equals(RESULT)) { // 成功
					// 更新单据为保存状态
//					_updateBillState(ctx, billId, 0, "");
				} else {
					return "ERROR";
				}
			} catch (com.ekingwin.www.esb.EAS_SUNAC_236_MaterialPurchasingPlan.QueryFault e) {
				e.printStackTrace();
				return "ERROR";
			} catch (RemoteException e) {
				e.printStackTrace();
				return "ERROR";
			} catch (ServiceException e) {
				e.printStackTrace();
				return "ERROR";
			}catch (EASBizException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return RESULT;
    }
    
    @Override
    protected void _updateBillState(Context ctx, String billId, int state,
    		String auditorNum) throws BOSException {
    	// TODO Auto-generated method stub
    	IMaterialPurchasingPlan localInstance = MaterialPurchasingPlanFactory.getLocalInstance(ctx);
    	try {
			String userID = ProcInstHelp.getAuditUserID(ctx, billId, "T_FDC_MaterialPurchasingPlan");
			if(userID != null && !userID.equals("")){
				UserInfo userInfo = UserFactory.getLocalInstance(ctx).getUserInfo(new ObjectUuidPK(userID));
				ContextUtil.setCurrentUserInfo(ctx, userInfo);
			}
			if (state == 0) {// 提交
//				StringBuffer ksql = new StringBuffer(
//						"update T_FDC_MaterialPurchasingPlan set FState='1SAVED' where fid='"
//						+ billId + "'");
//				DbUtil.execute(ctx, ksql.toString());
			} else if (state == 1) {// 审批中
				StringBuffer ksql = new StringBuffer(
						"update T_FDC_MaterialPurchasingPlan set FState='3AUDITTING' where fid='"
						+ billId + "'");
				DbUtil.execute(ctx, ksql.toString());
			} else if (state == 2) { // 已审批
				localInstance.audit(BOSUuid.read(billId));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BOSException(e.getMessage());
		}
    }
    
    @Override
    protected String _viewBPMWorkFlow(Context ctx, String billid)
    		throws BOSException {
    	// TODO Auto-generated method stub
    	String URL ="";
		URL = BPMViewUrlUtil.getBPMWFViewURL(ctx, "T_FDC_MaterialPurchasingPlan", billid);
    	return URL;
    }
}